    
SELECT DISTINCT ' ' CODICE_RECORD,UBI.ATT_ID,
UBI.ATT_IDEXT DEFINIZIONE_POD,
UBI.ELETOPO_DES VIA,
UBI.IND_NUM NUMERO,
UBI.CAP_ID CODICE_POSTALE,
UBI.COM_DES CITTA,'ITALIA' PAESE,
UBI.PRO_SIGLA,
/** POTENZA DISPONIBILE  **/
(SELECT max( ATTVAL.VCATT_VAL) FROM ue_APP_SWITCH_VATT ATTVAL 
WHERE UBI.ATT_ID=ATTVAL.ATT_ID
AND ATTVAL.CATT_DES='POTENZA DISPONIBILE') POTENZA_DISPONIBILE,
/** POTENZA DISPONIBILE  **/

/** POTENZA IMPEGNATA  **/
(SELECT max(ATTVAL.VCATT_VAL) FROM ue_APP_SWITCH_VATT ATTVAL 
WHERE UBI.ATT_ID=ATTVAL.ATT_ID
AND ATTVAL.CATT_DES='POTENZA IMPEGNATA') POTENZA_IMPEGNATA,
/** POTENZA IMPEGNATA  **/

/** TIPOLOGIA D'USO **/
CASE WHEN CON.TCON_DES='DOMESTICO' THEN 'IT_E_CO5'  
ELSE CASE WHEN CON.TCON_DES='ALTRI USI'  THEN 'IT_E_CO1'
ELSE CASE WHEN CON.TCON_DES='ILLUMINAZIONE PUBBLICA'  THEN 'IT_E_CO3' END END END  TIPOLOGIA_DUSO,
/** TIPOLOGIA D'USO **/

/** TENSIONE DI ALIMENTAZIONE  **/
CASE WHEN 
(SELECT (max(ATTVAL.CATT_VALVIS)) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='TENSIONE VOLT')='125V'
AND (SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='FASI')='MONOFASE' THEN '0001'
ELSE CASE WHEN  
(SELECT (max(ATTVAL.CATT_VALVIS)) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='TENSIONE VOLT')='220V'
AND (SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='FASI')='MONOFASE' THEN '0002'
ELSE CASE WHEN  
(SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='TENSIONE VOLT')='220V'
AND (SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='FASI')='TRIFASE' THEN '0003'  
ELSE CASE WHEN  
(SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='TENSIONE VOLT')='380V'
AND (SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='FASI')='TRIFASE' THEN '0004'  
ELSE CASE WHEN  
(SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='TENSIONE VOLT')='8.400V'
AND (SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='FASI')='TRIFASE' THEN '0005' 
ELSE CASE WHEN
(SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='TENSIONE VOLT')='20.000V'
AND (SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='FASI')='TRIFASE' THEN '0006' 
ELSE CASE WHEN
(SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='TENSIONE VOLT')='150.000V'
AND (SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='FASI')='TRIFASE' THEN '0008' 
ELSE CASE WHEN
(SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='TENSIONE VOLT')='15.000V'
AND (SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='FASI')='TRIFASE' THEN '0009' 
ELSE CASE WHEN
(SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='TENSIONE VOLT')='10.000V'
AND (SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='FASI')='TRIFASE' THEN '0010'
ELSE CASE WHEN
(SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='TENSIONE VOLT')='134.000V'
AND (SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='FASI')='TRIFASE' THEN '0011'
ELSE  '007'
END END END END END END END END END END TENSIONE_DI_ALIMENTAZIONE,

/** TENSIONE DI ALIMENTAZIONE  **/

/** LIVELLO DI TENSIONE **/
CASE WHEN 
(SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='TIPO FORNITURA EE')='BASSA TENSIONE'
  THEN 'IT_BT'
ELSE CASE WHEN  
(SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='TIPO FORNITURA EE')='MEDIA TENSIONE'
THEN 'IT_MT'
ELSE CASE WHEN  
(SELECT max(ATTVAL.CATT_VALVIS) FROM ue_APP_SWITCH_VATT ATTVAL WHERE UBI.ATT_ID=ATTVAL.ATT_ID AND ATTVAL.CATT_DES='TIPO FORNITURA EE')='ALTA TENSIONE'
THEN 'IT_AT'
END END END LIVELLO_DI_TENSIONE,
/** LIVELLO DI TENSIONE **/

/** DATI FISCALI**/
NVL(SOG_CODFIS,SOG.SOG_PARIVA) DATI_FISCALI,
/** DATI FISCALI**/

'01000' SETTORE_INDUSTRIALE,

/** RES/NORES **/
CASE WHEN ( SELECT VAL1.CCON_VALVIS FROM UE_APP_SWITCH_VALCUF VAL1 WHERE VAL1.CUF_ID=UBI.CUF_ID AND VAL1.CCON_ID=16 AND 
VAL1.VER_DTFINE=TO_DATE('31/12/9999','DD/MM/YYYY')  )='SI' THEN 'IT_RES'  
ELSE 'IT_NO_RES' END  RES_NORES,
/** RES/NORES **/
'' PRECISAZIONE,'' SUPPLEMENTO_1,'' SUPPLEMENTO_2,''PIANO,'' AULA,'' POSIZIONE,'' TIPO_EDIFICIO,

/** TIPO_SWITCH _WIN **/
CASE WHEN ( SELECT VAL1.CCON_VALVIS FROM UE_APP_SWITCH_VALCUF VAL1 WHERE VAL1.CUF_ID=UBI.CUF_ID AND VAL1.CCON_ID=21 AND 
VAL1.VER_DTFINE=TO_DATE('31/12/9999','DD/MM/YYYY')  )='VINCOLATO-LIBERO' THEN 'VL'  
ELSE 'LL' END  TIPO_SWITCH_WIN,
/** TIPO_SWITCH_WIN **/

/** CONSUMO_TOTALE  FU**/
(SELECT FRF.FU
FROM  UE_ASP_CONSANNO_ELE_PROV frf
WHERE  UBI.CUF_ID=FRF.CUF_ID
) CONSUMO_FU,
/** CONSUMO_TOTALE FU**/

/** ANNO RIFERIMENTO**/
--da programma
/** ANNO RIFERIMENTO**/
/** MESE RIFERIMENTO**/
--da programma
/** MESE RIFERIMENTO**/
/**CODICE FASCIA**/
--DA PROGRAMMA
/**CODICE FASCIA**/
/**QUANTITA F1**/
(SELECT FRF.F1
FROM  UE_ASP_CONSANNO_ELE_PROV frf
WHERE  UBI.CUF_ID=FRF.CUF_ID
) CONSUMO_F1,

/** QUANTITA F1**/

/** QUANTITA F2**/
(SELECT FRF.F2
FROM  UE_ASP_CONSANNO_ELE_PROV frf
WHERE  UBI.CUF_ID=FRF.CUF_ID
) CONSUMO_F2,
/** QUANTITA F2 **/


/** QUANTITA F3**/
(SELECT FRF.F3
FROM  UE_ASP_CONSANNO_ELE_PROV frf
WHERE  UBI.CUF_ID=FRF.CUF_ID
) CONSUMO_F3,

/** QUANTITA F3 **/

(SELECT O.OT_DESSTA   FROM   V_CUFOT  CU,
                  OT       O
           WHERE CU.STA_IDSYS = 1
           AND CU.CUF_ID=UBI.CUF_ID        
           AND CU.VER_DTFINE = TO_DATE ('31/12/9999','DD/MM/YYYY')
           AND O.OT_ID = CU.OT_ID
           AND O.SER_ID = '03'
           AND O.CATCOMSER_ID IN (7,10)
           AND O.COMSER_ID IN (16,27) ) AS OT_VENDITA

FROM UE_APP_SWITCH_CON CON,UE_APP_SWITCH_UBI UBI,UE_APP_SWITCH_VALCUF VAL,--V_ATTGMIS GMIS,
UE_APP_SWITCH_SOG SOG
WHERE CON.CON_ANNO=UBI.CON_ANNO
AND CON.CON_ID=UBI.CON_ID
AND UBI.CUF_ID=VAL.CUF_ID
--AND UBI.ATT_ID=GMIS.ATT_ID
AND CON.SOG_ID=SOG.SOG_ID
AND VAL.CCON_DES='DATA PRESUNTA ATTIVAZIONE'
--and ubi.cuf_id=61572
--and ubi.cuf_id=61572
AND VAL.VCCUF_VAL=^Digitare data di Switch gg/mm/aaaa;A;ALFABETICO^^--'01/07/2012' /** INSERIRE UN MENU' A TENDINA CON LE DAT EDI INIZIO MESE **/
ORDER BY UBI.ATT_ID DESC			